import api from './api';
import { Course } from '@/types';

// 接口返回类型
interface ApiResponse<T = any> {
  code: number;
  message: string;
  data?: T;
}

interface CourseListResponse {
  courses: Course[];
}

// 创建课程
export const createCourse = async (name: string, className: string, description: string, classId?: number): Promise<ApiResponse> => {
  try {
    console.log('创建课程:', { name, className, description, classId });
    
    // 构建请求数据
    const requestData: any = { 
      name,
      description 
    };
    
    // 如果提供了班级ID，优先使用班级ID
    if (classId) {
      requestData.class_id = classId;
    } else {
      // 否则使用班级名称
      requestData.class_name = className;
    }
    
    const response = await api.post('/teacher/course/create', requestData);
    console.log('创建课程响应:', response);
    return response;
  } catch (error) {
    console.error('创建课程错误:', error);
    throw error;
  }
};

// 获取所有课程列表 (用于签到统计)
export const getCourses = async (): Promise<ApiResponse<CourseListResponse>> => {
  try {
    console.log('获取所有课程');
    const response = await api.get('/teacher/course/list');
    console.log('获取所有课程响应:', response);
    
    // 返回标准格式的响应
    return response;
  } catch (error) {
    console.error('获取所有课程错误:', error);
    throw error;
  }
};

// 获取我的课程列表
export const getMyCourses = async (): Promise<ApiResponse<CourseListResponse>> => {
  try {
    console.log('获取我的课程');
    const response = await api.get('/teacher/course/list');
    console.log('获取我的课程响应:', response);
    
    // 返回标准格式的响应
    return response;
  } catch (error) {
    console.error('获取我的课程错误:', error);
    throw error;
  }
};

// 获取教师的课程列表 - 统一使用/teacher/course/list接口
export const getCourseList = async (_teacherId?: string): Promise<ApiResponse<CourseListResponse>> => {
  try {
    // 忽略teacherId参数，直接使用统一的课程列表接口
    console.log('获取课程列表');
    const response = await api.get('/teacher/course/list');
    console.log('获取课程列表响应:', response);
    return response;
  } catch (error) {
    console.error('获取课程列表错误:', error);
    throw error;
  }
};

// 获取单个课程详情
export const getCourseById = async (id: number): Promise<ApiResponse<Course>> => {
  try {
    console.log('获取课程详情:', id);
    const response = await api.get(`/teacher/course/detail?id=${id}`);
    console.log('获取课程详情响应:', response);
    return response;
  } catch (error) {
    console.error('获取课程详情错误:', error);
    throw error;
  }
};

// 更新课程
export const updateCourse = async (id: number, name: string, className: string, description: string): Promise<ApiResponse> => {
  try {
    console.log('更新课程:', { id, name, className, description });
    const response = await api.post('/teacher/course/update', { 
      id, 
      name, 
      class_name: className,
      description 
    });
    console.log('更新课程响应:', response);
    return response;
  } catch (error) {
    console.error('更新课程错误:', error);
    throw error;
  }
};

// 删除课程
export const deleteCourse = async (id: number): Promise<ApiResponse> => {
  try {
    console.log('删除课程:', id);
    // 注意：这里使用查询参数而不是JSON体
    const response = await api.post(`/teacher/course/delete?id=${id}`);
    console.log('删除课程响应:', response);
    return response;
  } catch (error) {
    console.error('删除课程错误:', error);
    throw error;
  }
};

// 获取课程学生列表
export const getCourseStudents = async (courseId: number): Promise<ApiResponse<{ students: any[] }>> => {
  try {
    console.log('获取课程学生列表:', courseId);
    const response = await api.get(`/teacher/course_student/list?course_id=${courseId}`);
    console.log('获取课程学生列表响应:', response);
    return response;
  } catch (error) {
    console.error('获取课程学生列表错误:', error);
    throw error;
  }
};

// 添加学生到课程
export const addStudentsToCourse = async (
  courseId: number, 
  studentIds: number[], 
  remark: string = ''
): Promise<ApiResponse> => {
  try {
    console.log('添加学生到课程:', { courseId, studentIds, remark });
    const response = await api.post('/teacher/course_student/add', {
      course_id: courseId,
      student_ids: studentIds,
      remark
    });
    console.log('添加学生到课程响应:', response);
    return response;
  } catch (error) {
    console.error('添加学生到课程错误:', error);
    throw error;
  }
};

// 从课程中移除学生
export const removeStudentFromCourse = async (courseId: number, studentId: number): Promise<ApiResponse> => {
  try {
    console.log('从课程中移除学生:', { courseId, studentId });
    const response = await api.post('/teacher/course_student/remove', {
      course_id: courseId,
      student_id: studentId
    });
    console.log('从课程中移除学生响应:', response);
    return response;
  } catch (error) {
    console.error('从课程中移除学生错误:', error);
    throw error;
  }
}; 